package com.example.ll_video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.ll_video.MyApplication;
import com.example.ll_video.bean.ResultData;
import com.example.ll_video.bean.UserInfo;
import com.example.ll_video.constant.Constant;
import com.example.ll_video.utils.AESUtil;
import com.example.ll_video.utils.DevicesUtil;
import com.example.ll_video.utils.HttpUtils;
import com.example.ll_video.utils.JsonUtil;
import com.example.ll_video.utils.SPUtils;
import com.example.ll_video.R;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

public class SplashActivity extends AppCompatActivity {
    private TextView tvTime;
    CountDownTimer timer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity_math);
        QMUIStatusBarHelper.isFullScreen(this);

        tvTime = findViewById(R.id.tv_time);



        /**
         * CountDownTimer timer = new CountDownTimer(3000, 1000)中，
         * 第一个参数表示总时间，第二个参数表示间隔时间。
         * 意思就是每隔一秒会回调一次方法onTick，然后1秒之后会回调onFinish方法。
         */
        timer = new CountDownTimer(3000, 1000) {
            public void onTick(long millisUntilFinished) {
                tvTime.setText(String.format("%d秒", millisUntilFinished / 1000));
            }

            public void onFinish() {
                Intent intent = new Intent(SplashActivity.this, NewMainActivity.class);
                startActivity(intent);
                finish();

            }
        };
        //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
        getLimit();
        getLimit2();

    }

    public void getLimit(){
        HttpUtils.get(Constant.NEW_LIMIT, new HttpUtils.HttpCallBackNew() {
            @Override
            public void onSuccess(String resultData) {
                if (resultData.equals("true")){
                    timer.start();
                }else {
                    finish();
                }

            }

            @Override
            public void onFail(String error) {
                finish();
            }
        });
    }

    public void getLimit2(){
        String devicesId  = DevicesUtil.getSerialNumber(SplashActivity.this);
        String source  = Constant.SOURCE;

        StringBuilder builder = new StringBuilder();
        builder.append("appinit?devicesId=").append(devicesId);
        builder.append("|").append("source=").append(source);
        builder.append("|").append("client=").append("andr");

        String url = Constant.START_APP +"?"+ AESUtil.encrypt(builder.toString());

        HttpUtils.get(url, new HttpUtils.HttpCallBack() {

            @Override
            public void onSuccess(ResultData resultData) {
                if (!resultData.getMsg().equals("err")) {

                }
            }

            @Override
            public void onFail(String error) {
                finish();
            }
        });
    }
}
